package cn.ronghuanet.service.impl;

import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.domain.Department;
import cn.ronghuanet.mapper.DepartmentMapper;
import cn.ronghuanet.query.DepartmentQuery;
import cn.ronghuanet.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
    }

    @Transactional
    @Override
    public void delete(Serializable id) {

        departmentMapper.delete(id);
//        System.out.println(1/0);
    }

    @Transactional
    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public Department selectById(Serializable id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);

    }

    @Override
    public PageInfo<Department> queryPage(DepartmentQuery departmentQuery) {
        // 高级查询和分页 只需要查 分页的数据 list = 10条 就OK了???  还要查total
        PageInfo<Department> pageInfo = new PageInfo<>();
        // 先查 total
        Long total = departmentMapper.queryTotal(departmentQuery);
        if (total != 0){
            List<Department> list = departmentMapper.querypage(departmentQuery);
            pageInfo.setList(list);
            pageInfo.setTotal(total);
        }
        // 在查 list
        return pageInfo;
    }
}
